<template>
  <div class="result-container">
    <!-- 顶部栏 -->
    <van-nav-bar
      :title="`${$route.query.keyword} 的搜索结果`"
      left-arrow
      @click-left="$router.back()"
      fixed
    />
    <!-- 搜索结果列表 -->
    <van-list
      class="search-list"
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="item in list"
        :key="item.art_id.toString()"
        :title="item.title"
        @click="$router.push({ path: '/article', query: { id: item.art_id } })"
      />
    </van-list>
  </div>
</template>

<script>
// 导入接口
import { getSearch } from '@/api/home.js'
export default {
  name: 'searchResult',
  data () {
    return {
      loading: false, // 加载更多动画
      finished: false, // 加载完毕状态
      list: [],
      page: 1 // 当前页数
    }
  },
  methods: {
    async onLoad () {
      // 1.发送请求
      const res = await getSearch({
        page: this.page,
        per_page: 10,
        q: this.$route.query.keyword
      })
      console.log(res)
      // 页码自增
      this.page++
      // 2.添加数据
      this.list.push(...res.data.data.results)
      // 3.完成本次加载
      this.loading = false
      // 4.判断是否加载完毕(如果全部加载完毕，服务器会返回空数组)
      if (res.data.data.results.length === 0) {
        this.finished = true
      }
    }
  }
}
</script>

<style lang="less">

.result-container {
  height: 100vh;
  padding-top: 46px;
  box-sizing: border-box;
  .search-list {
    height: 100%;
    overflow-y: scroll;
  }
}

</style>
